package com.xidian.bankdemo.util;

import io.jsonwebtoken.*;
import java.util.Date;

public class TokenUtil {
    private static final int EXPIRATION_TIME = 12*3600*1000;
    private static final String SECRET_KEY = "027*9ee65814fc0cdb860474339a9cc9d039a75a45e3bdc63c3c5083a67c9646b";
    public static final String TOKEN_PREFIX = "Bearer ";
    public static final String AUTHORIZATION = "Authorization";

    public static String generateToken(String username){
        return TOKEN_PREFIX + Jwts.builder()
                .setSubject(username)
                .claim("username", username)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME ))
                .signWith(SignatureAlgorithm.HS256, SECRET_KEY)
                .compact();
    }
    public static String validateToken(String jwtToken){
        Claims claims = Jwts.parser()
                .setSigningKey(SECRET_KEY)
                .parseClaimsJws(jwtToken.replace(TOKEN_PREFIX, ""))
                .getBody();
        return claims.get("username").toString();
    }
}
